package com.ht.impl;

import com.ht.impl.editor.BasicGraphEditor;
import com.ht.impl.editor.EditorActions;
import com.mxgraph.swing.util.mxGraphActions;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraphView;
import com.mxgraph.util.mxResources;
import com.mxgraph.util.mxEventSource;
import com.mxgraph.util.mxEventObject;
import com.mxgraph.util.mxEvent;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2010-7-1
 */
public class ViewerToolBar extends JToolBar {
    private boolean ignoreZoomChange = false;

    public ViewerToolBar(final BasicGraphEditor editor, int orientation) {
        super(orientation);
        setBorder(BorderFactory.createCompoundBorder(BorderFactory
                .createEmptyBorder(3, 3, 3, 3), getBorder()));
        setFloatable(false);

//         add(editor.bind(mxResources.get("openFile"), new EditorActions.OpenAction(),
//                "/icons/openedblue.png"));
        add(editor.bind("Save", new EditorActions.SaveAction(false),
                "/com/mxgraph/examples/swing/images/save.gif"));

        addSeparator();

        add(editor.bind("Print", new EditorActions.PrintAction(),
                "/com/mxgraph/examples/swing/images/print.gif"));

        addSeparator();

        add(editor.bind("zoomIn", mxGraphActions.getZoomInAction(),
                "/com/mxgraph/examples/swing/images/zoomin.gif"));

        add(editor.bind("zoomIn", mxGraphActions.getZoomOutAction(),
                "/com/mxgraph/examples/swing/images/zoomout.gif"));
        addSeparator();
        final mxGraphView view = editor.getGraphComponent().getGraph()
                .getView();
        final JComboBox zoomCombo = new JComboBox(new Object[]{"400%",
                "200%", "150%", "100%", "75%", "50%", mxResources.get("page"),
                mxResources.get("width"), mxResources.get("actualSize")});
        zoomCombo.setEditable(true);
        zoomCombo.setMinimumSize(new Dimension(75, 0));
        zoomCombo.setPreferredSize(new Dimension(75, 0));
        zoomCombo.setMaximumSize(new Dimension(75, 100));
        zoomCombo.setMaximumRowCount(9);
        add(zoomCombo);

        // Sets the zoom in the zoom combo the current value
        mxEventSource.mxIEventListener scaleTracker = new mxEventSource.mxIEventListener() {
            /**
             *
             */
            public void invoke(Object sender, mxEventObject evt) {
                ignoreZoomChange = true;

                try {
                    zoomCombo.setSelectedItem((int) Math.round(100 * view
                            .getScale()) + "%");
                }
                finally {
                    ignoreZoomChange = false;
                }
            }
        };

        // Installs the scale tracker to update the value in the combo box
        // if the zoom is changed from outside the combo box
        view.getGraph().getView().addListener(mxEvent.SCALE,
                scaleTracker);
        view.getGraph().getView().addListener(
                mxEvent.SCALE_AND_TRANSLATE, scaleTracker);

        // Invokes once to sync with the actual zoom value
        scaleTracker.invoke(null, null);

        zoomCombo.addActionListener(new ActionListener() {
            /**
             *
             */
            public void actionPerformed(ActionEvent e) {
                mxGraphComponent graphComponent = editor.getGraphComponent();

                // Zoomcombo is changed when the scale is changed in the diagram
                // but the change is ignored here
                if (!ignoreZoomChange) {
                    String zoom = zoomCombo.getSelectedItem().toString();

                    if (zoom.equals(mxResources.get("page"))) {
                        graphComponent.setPageVisible(true);
                        graphComponent
                                .setZoomPolicy(mxGraphComponent.ZOOM_POLICY_PAGE);
                    } else if (zoom.equals(mxResources.get("width"))) {
                        graphComponent.setPageVisible(true);
                        graphComponent
                                .setZoomPolicy(mxGraphComponent.ZOOM_POLICY_WIDTH);
                    } else if (zoom.equals(mxResources.get("actualSize"))) {
                        graphComponent.zoomActual();
                    } else {
                        try {
                            zoom = zoom.replace("%", "");
                            graphComponent.zoomTo(
                                    Double.parseDouble(zoom) / 100,
                                    graphComponent.isCenterZoom());
                        }
                        catch (Exception ex) {
                            JOptionPane.showMessageDialog(editor, ex
                                    .getMessage());
                        }
                    }
                }
            }
        });
         addSeparator();
    }
}

